package com.ldc.message.fragment

import android.os.Bundle
import android.os.Message
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.ldc.commonlibs.conf.AppConf
import com.ldc.commonlibs.conf.RouteConf
import com.ldc.commonlibs.listener.OnRowListener
import com.ldc.commonlibs.uts.ApiUtils
import com.ldc.commonlibs.uts.MessageUts
import com.ldc.commonlibs.uts.ViewModelUtils
import com.ldc.commonres.common.VBMFragment
import com.ldc.message.adapter.MessageListAdapter
import com.ldc.message.databinding.MessageFragmentReadBinding
import com.ldc.message.model.MessageModel
import com.ldc.message.viewmodel.MessageViewModelBase
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener


class ReadFragment : VBMFragment<MessageFragmentReadBinding, MessageViewModelBase>() {
    private var messageAdapter: MessageListAdapter? = null
    private var page: Int = 0

    companion object {
        private const val REFRESH_CODE = 0x123123
        fun newInstance(data: Bundle = Bundle()): ReadFragment = ReadFragment().apply {
            arguments = data
        }
    }

    override fun processMessage(msg: Message): Boolean {
        when (msg.what) {
            REFRESH_CODE -> {
                getMessageList()
            }
        }
        return super.processMessage(msg)
    }


    override fun main() {
        viewBind?.statusView?.let {
            it.showLoadView()
            it.addRetryClickListener(object : OnRowListener<Int> {
                override fun onRow(v: View, data: Int) {
                    MessageUts.sendMessage(getHandler(), REFRESH_CODE)
                }
            })
        }
        initAdapter()
        viewBind?.contentView?.also { cv ->
            cv.refreshView.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
                override fun onRefresh(refreshLayout: RefreshLayout) {
                    page = 0
                    MessageUts.sendMessage(getHandler(), REFRESH_CODE)
                }

                override fun onLoadMore(refreshLayout: RefreshLayout) {
                    page += 1
                    MessageUts.sendMessage(getHandler(), REFRESH_CODE)
                }
            })
        }
        MessageUts.sendMessage(getHandler(), REFRESH_CODE)
    }

    override fun createViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): MessageFragmentReadBinding = MessageFragmentReadBinding.inflate(inflater, container, false)

    override fun createViewModel(): MessageViewModelBase =
        ViewModelUtils.create(this, MessageViewModelBase::class.java)

    private fun initAdapter() {
        messageAdapter = MessageListAdapter().also {
            it.addOnRowListener(object : OnRowListener<MessageModel> {
                override fun onRow(v: View, data: MessageModel) {
                    ARouter.getInstance()
                        .build(RouteConf.WebViewRoute.startEnter)
                        .withString(AppConf.ARG_TITLE, data.title)
                        .withString(AppConf.ARG_URL, data.fullLink)
                        .navigation()
                }
            })
        }
        viewBind?.contentView?.rv?.also { rv ->
            rv.recyclerview.layoutManager =
                LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false)
            rv.recyclerview.setHasFixedSize(true)
            rv.recyclerview.adapter = messageAdapter
        }

    }

    // 获取数据
    private fun getMessageList() {
        viewModel?.readedList(page)?.observe(this) {
            viewBind?.statusView?.showContentView()
            viewBind?.contentView?.refreshView?.also { rv ->
                if (rv.state.isOpening) {
                    rv.finishRefresh()
                    rv.finishLoadMore()
                }
                rv.setNoMoreData(page == it.data?.pageCount)
            }
            if (ApiUtils.isSuccess(it)) {
                if (page == 0) {
                    messageAdapter?.newData(it.data?.datas)
                } else {
                    messageAdapter?.addNewData(it.data?.datas)
                }
            } else {
                viewBind?.statusView?.showErrorView(it.errorMsg)
            }
            if (messageAdapter?.isEmptyData()!!) {
                viewBind?.statusView?.showEmptyView()
            }
        }
    }


}